﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using TruyenCuoiViet.ViewModels;
using TruyenCuoiViet.Source.Helper;

namespace TruyenCuoiViet.Source.UI
{
    public partial class ReadNews : PhoneApplicationPage
    {
        public static ReadNews instance;
        public int newsId = 0;
        public int categoryId = 0;
        public ReadNews()
        {
            InitializeComponent();
            instance = this;
        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (NavigationContext.QueryString.ContainsKey("NewsId"))
            {
                ReadNews.instance.pgbLoad.Visibility = Visibility.Visible;
                categoryId = int.Parse(NavigationContext.QueryString["CategoryId"]);
                CategoryViewModel c = App.ViewModel.FindCategory(categoryId);
                newsId = int.Parse(NavigationContext.QueryString["NewsId"]);
                ItemViewModel nv = c.FindItems(newsId);
                LoadNews(nv);
            }
        }

        private async void LoadNews(ItemViewModel nv)
        {
            if (nv == null)
                return;
            ReadNews.instance.pgbLoad.Visibility = Visibility.Visible;
            newsId = nv.Id;
            tbltitle.Text = nv.Title;
            string n = await NewsHelper.getContent(nv);
            webNewsDetail.NavigateToString(n);
            ReadNews.instance.pgbLoad.Visibility = Visibility.Collapsed;
            TransitionHelper.excuteTransition(webNewsDetail);
        }

        private void BtnNext_Click(object sender, System.EventArgs e)
        {
            ItemViewModel nv = App.ViewModel.FindCategory(categoryId).FindNextItems(newsId);
            if (nv == null)
                MessageBox.Show("Không tìm thấy truyện kế tiếp.");
            else
                LoadNews(nv);
        }

        private void btnPrev_Click(object sender, System.EventArgs e)
        {
            ItemViewModel nv = App.ViewModel.FindCategory(categoryId).FindPrevItems(newsId);
            if (nv == null)
                MessageBox.Show("Không tìm thấy truyện trước.");
            else
                LoadNews(nv);
        }
        protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
        {
            PhoneApplicationPage phoneApplicationPage = (PhoneApplicationPage)(((PhoneApplicationFrame)Application.Current.RootVisual)).Content;
            TransitionHelper.excuteTransition(phoneApplicationPage);
            base.OnNavigatingFrom(e);
        }

        private void btnShare_Click(object sender, EventArgs e)
        {
            ItemViewModel nv = App.ViewModel.FindCategory(categoryId).FindItems(newsId);
            ShareLinkTask shareLinkTask = new ShareLinkTask();
            shareLinkTask.LinkUri = new Uri(nv.DetailUrl, UriKind.Absolute);
            shareLinkTask.Message = nv.Title;
            shareLinkTask.Title = nv.Title;
            shareLinkTask.Show();
        }
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            categoryId = int.Parse(NavigationContext.QueryString["CategoryId"]);
            NavigationService.Navigate(new Uri("/Source/UI/MainPage.xaml?CategoryId=" + categoryId, UriKind.Relative));
        }
    }
}